//
//  ComSettingController.m
//  ProjectA
//
//  Created by lanouhn on 15/5/23.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "ComSettingController.h"

#import <MessageUI/MFMailComposeViewController.h>
#import "UIImageView+WebCache.h"
#import <UIKit/UIKit.h>


@interface ComSettingController ()<MFMailComposeViewControllerDelegate>
@property (nonatomic ,retain)UILabel *lable;
@property (nonatomic ,retain)NSArray *dataSource;
@property (nonatomic ,retain)UILabel *accessorylable;
@end

@implementation ComSettingController
- (void)dealloc{
    [_lable release];
    [_dataSource release];
    [_accessorylable release];
    [super dealloc];
}
- (NSArray *)dataSource{
    if (!_dataSource) {
        self.dataSource = [NSArray arrayWithObjects:@"意见反馈",@"清除缓存",@"版本信息", nil];
    }
    return _dataSource;
}
- (UILabel *)accessorylable {
    if (!_accessorylable) {
        self.accessorylable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]autorelease];
        _accessorylable.text = @"V1.1";
        
    }
    return _accessorylable ;
}
- (UILabel *)lable {
    if (!_lable) {
        self.lable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]autorelease];
        _lable.text = @"更多";
        _lable.textAlignment = NSTextAlignmentCenter;
        _lable.textColor = [UIColor whiteColor];
    }
    return _lable ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem .titleView = self.lable;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 2) {
        cell.accessoryView = self.accessorylable;
    }
    cell.textLabel.text = self.dataSource[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil)
        {
            if ([mailClass canSendMail])
            {
                [self displayMailPicker];
            }
        }
    }else if(indexPath.row == 1){
        
        CGFloat folderSize = 0;
//        NSLog(@"%ld", [[SDImageCache sharedImageCache] getSize]);
        folderSize = [[SDImageCache sharedImageCache] getSize] / 1024.0 / 1024.0;
        [[SDImageCache sharedImageCache] clearDisk];
        NSString *str = [NSString stringWithFormat:@"帮您整理了%.2f兆缓存",folderSize];
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alterView show];
        [alterView release];
        /*
                dispatch_async(
                       
                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                       , ^{
                           
                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                           
                           for (NSString *p in files) {
                               
                               NSError *error;
                               
                               NSString *path = [cachPath stringByAppendingPathComponent:p];
                               
                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                   
                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                                   
                               }
                               
                           }
         
                           [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
         */
    }
}

//-(void)clearCacheSuccess
//{
//    CGFloat folderSize;
//    NSLog(@"%ld", [[SDImageCache sharedImageCache] getSize]);
//    folderSize = [[SDImageCache sharedImageCache] getSize] / 1024.0 / 1024.0;
//    NSString *str = [NSString stringWithFormat:@"帮您整理了%.2f兆缓存",folderSize];
//  UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//    [alterView show];
//    [alterView release];
//}

//调出邮件发送窗口
- (void)displayMailPicker
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    
    //设置主题
    [mailPicker setSubject: @"反馈"];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @"1272553299@qq.com"];
    [mailPicker setToRecipients: toRecipients];
    
    NSString *emailBody = @"正文：";
    [mailPicker setMessageBody:emailBody isHTML:YES];
    [self presentViewController:mailPicker animated:YES completion:nil];
    [mailPicker release];
    
    
}

#pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:nil];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msg = @"用户成功保存邮件";
            break;
        case MFMailComposeResultSent:
            msg = @"发送成功！";
            break;
        case MFMailComposeResultFailed:
            msg = @"用户试图保存或者发送邮件失败";
            break;
        default:
            msg = @"";
            break;
    }
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alterView show];
    [alterView release];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
